package com.am.util;

import com.am.entity.Canvas;
import com.am.entity.OperateReq;
import com.am.entity.Shape;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

/**
 * 用户对画布的操作
 */
public class ShapeUtil {

    /**
     * 添加绘画操作
     * @param canvas
     * @param req
     */
    public static  void  addShape(Canvas canvas, OperateReq req){
        //将当前操作添加到操作列表中
        canvas.getShapes().add(req.getShape());
        //将当前操作添加到 当前用户的操作中，便于撤销操作
        HashMap<String, UserShapes> userShapes = canvas.getUserShapes();
        if (userShapes.containsKey(req.getUid())){//用户不是第一次操作
            UserShapes userShape = userShapes.get(req.getUid());
            userShape.list.add(req.getShape());
        }else {
            List<Shape> list = new ArrayList<>();
            list.add(req.getShape());
            userShapes.put(req.getUid(), new UserShapes(list, new Stack<>()));
        }
    }

    /**
     * 用户撤销操作
     * @param canvas
     * @param req
     * @return
     */
    public static boolean undoShape(Canvas canvas, OperateReq req) {
        //得到每个用户的操作列表
        HashMap<String, UserShapes> userShapes = canvas.getUserShapes();
        String uid = req.getUid();
        if (!userShapes.containsKey(uid)){//该用户没有在此话画布上进行过操作
            return false;
        }
        //用户撤销操作(最小只能到0)
        UserShapes userShape = userShapes.get(uid);
        List<Shape> list = userShape.list;
        if (list.size() <= 0){//已经撤销完了，不能再撤销了
            return false;
        }else {
            userShape.stack.add(list.get(list.size() - 1));
            list.remove(list.size() - 1);
        }

        //将现有的操作添加到集合中
        List<Shape> shapeList = newShapeList(userShapes);

        //覆盖原来的操作
        canvas.setShapes(shapeList);

        return true;
    }

    /**
     * 用户取消撤销操作
     * @param canvas
     * @param req
     * @return
     */
    public static boolean redoShape(Canvas canvas, OperateReq req) {
        //得到每个用户的操作列表
        HashMap<String, UserShapes> userShapes = canvas.getUserShapes();
        String uid = req.getUid();
        if (!userShapes.containsKey(uid)){//该用户没有在此话画布上进行过操作
            return false;
        }
        //用户取消撤销操作(下标最大只能到size)
        UserShapes userShape = userShapes.get(uid);
        List<Shape> list = userShape.list;
        if (userShape.stack.isEmpty()){//撤销的栈为空（已经撤销不了了）
            return false;
        }else {
            list.add(userShape.stack.pop());
        }

        //将现有的操作添加到集合中
        List<Shape> shapeList = newShapeList(userShapes);

        //覆盖原来的操作
        canvas.setShapes(shapeList);

        return true;
    }
    /**
     * 从每个用户操作的列表中组合出一个完成的操作
     * @param userShapes
     * @return
     */
    public static List<Shape> newShapeList(HashMap<String, UserShapes> userShapes){
        List<Shape> shapeList = new ArrayList<>();

        for(String userName : userShapes.keySet()){
            UserShapes userShapes1 = userShapes.get(userName);
            shapeList.addAll(userShapes1.list);
        }
        return shapeList;
    }
}
